<?php
require_once dirname(__FILE__) . '/Config.inc.php';
require_once dirname(__FILE__) . '/Form.class.php';
require_once dirname(__FILE__) . '/Model.class.php';
require_once dirname(__FILE__) . '/Soap.class.php';
require_once dirname(__FILE__) . '/components/Cookie.class.php';
require_once dirname(__FILE__) . '/components/Session.class.php';
require_once dirname(__FILE__) . '/smarty/Smarty.class.php';

/**
 * 是否回传
 * @var boolean
 */
define('IS_POSTBACK', $_SERVER["REQUEST_METHOD"] == "POST");

/**
 *
 * @author Kinmeic
 *        
 */
abstract class Controller {
	protected $view;
	protected $session;
	protected $cookie;
	protected $response;
	
	//频繁使用属性
	protected $page;
	protected $action;
	
	// Force Extending class to define this method
	protected abstract function init();
	protected abstract function onPostBack();
	protected abstract function onShow();
	
	function __construct() {
		global $Config;
		
		if (isset($Config) && isset($Config['db'])) {
			$this->view = new Smarty();
			$this->view->setCacheDir($Config['view']['cache_dir']);
			$this->view->setCompileDir($Config['view']['compile_dir']);
			$this->view->setTemplateDir($Config['view']['template_dir']);
			$this->view->cache_lifetime = 60*60*24;
			$this->view->caching = false;
		} else {
			exit('Fatal error: View configuration does not found in Config.inc.php.');
		}
		
		$this->cookie = Cookie::getInstance();
		$this->session = Session::getInstance();
		
		//回复消息包
		$this->response = array(
				"code" => 0,
				"message" => '',
				);
		
		$this->page = $this->request('page', 1);
		$this->action = $this->request('action');
		
	}
	
	function __destruct() {
		
		// TODO - Insert your code here
	}
	
	/**
	 * @return string
	 */
	function __toString() {
		return json_encode($this->response);
	}
	
	/**
	 * 运行控制器实例
	 */
	static function run() {
		$instance = new static();
		$instance->init();
		
		if ($_SERVER["REQUEST_METHOD"] == "POST") {
			$instance->onPostBack();
		} else {
			$instance->onShow();
		}
	}
	
	/**
	 * @param int $code
	 */
	function setCode($code) {
		$this->response["code"] = $code;
	}

	/**
	 * @param string $message
	 */
	function setMessage($message) {
		$this->response["message"] = $message;
	}
	
	/**
	 * @param mixed $content
	 * @param int $total default 0
	 * @param int $page defalut 1
	 */
	function setData($content, $total = 0) {
		$this->response['content'] = $content;
		$this->response['total'] = $total;
	}
	
	/**
	 * @param mixed $extra
	 */
	function setExtra($extra) {
		$this->response["extra"] = $extra;
	}
	
	/**
	 * @param string $name
	 * @param string $default
	 * @return mixed
	 */
	function post($name, $default = "") {
		return isset($_POST[$name]) ? $_POST[$name] : $default;
	}
	
	/**
	 * @param string $name
	 * @param string $default
	 * @return mixed
	 */
	function get($name, $default = "") {
		return isset($_GET[$name]) ? $_GET[$name] : $default;
	}
	
	/**
	 * @param string $name
	 * @param string $default
	 * @return mixed
	 */
	function request($name, $default = "") {
		return isset($_REQUEST[$name]) ? $_REQUEST[$name] : $default;
	}
	
	/**
	 * @param string $url
	 */
	function jump($url) {
		header("location:" . $url);
	}
}

?>